package org.arrow.tap.servlet;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * Configures log4j mostly
 */
public class ConfigurationServlet extends HttpServlet implements
		ServletContextListener {

	/** Comment for <code>serialVersionUID</code> */
	private static final long serialVersionUID = 3257005471045858873L;
	
	/* Logger */
	private static final Logger log = Logger.getLogger(ConfigurationServlet.class);
	
	// private static Log log = LogFactory.getLog(Insert.class);

	/**
	 * @see javax.servlet.GenericServlet#init()
	 */
	public void init() {
		try {
			// Use basic logging configuration until Log4j is properly
			// configured
			PropertyConfigurator.configure(this.getServletContext()
					.getRealPath("/")
					+ "/WEB-INF/log4j.properties");
			log.info("setup log4j OK");
			
		} catch (Throwable t) {
			t.printStackTrace();
			log.fatal("Config failed", t);
		}
	}

	/**
	 * @see javax.servlet.Servlet#destroy()
	 */
	public void destroy() {
		super.destroy();
		org.apache.log4j.LogManager.shutdown();
	}

	/**
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent arg0) {
		org.apache.log4j.LogManager.shutdown();
	}

	/**
	 * {@inheritDoc}
	 */
	public void contextInitialized(ServletContextEvent arg0) {
		log.info("begin schedule");
	}

}
